package com.group.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import com.group.admin.dao.ShopUserDao;
import com.group.admin.entity.ShopUserEntity;
import com.group.common.jwt.ClaimsStorage;
import com.group.common.utils.R;

import com.xxl.job.core.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group.common.utils.PageUtils;
import com.group.common.utils.Query;

import com.group.admin.dao.ShopCheckDao;
import com.group.admin.entity.ShopCheckEntity;
import com.group.admin.service.ShopCheckService;


@Service("shopCheckService")
public class ShopCheckServiceImpl extends ServiceImpl<ShopCheckDao, ShopCheckEntity> implements ShopCheckService {

    @SuppressWarnings("all")
    @Autowired
    ShopUserDao shopUserDao;
    @SuppressWarnings("all")
    @Autowired
    ShopCheckDao shopCheckDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ShopCheckEntity> page = this.page(
                new Query<ShopCheckEntity>().getPage(params),
                new QueryWrapper<ShopCheckEntity>()
        );

        return new PageUtils(page);
    }
    @Override
    public R check(Object user) {

        HashMap<String,String> a = ClaimsStorage.a;
        String s1 = a.get("id");
        System.out.println("id:"+s1);
        String s = JSON.toJSONString(user);
        ShopUserEntity shopUserEntity = JSONObject.parseObject(s, new TypeReference<ShopUserEntity>() {
        });
        if (shopUserEntity==null){
            return R.error("用户未登录，请登录再签到");
        }
        //获得当前登录用户id
        Integer userid = shopUserEntity.getUserId();
        ShopCheckEntity byId = this.getById(userid.toString());
        //获取当前时间，拿出当前时间拼接三位数int值
        String datestring = DateUtil.format(new Date(), "MMdd");//当前签到日期
        int nowdate = Integer.parseInt(datestring);


        if (byId==null){
            //用户第一次签到
            ShopCheckEntity shopCheckEntity = new ShopCheckEntity();
            shopCheckEntity.setContinuitySign(1);
            shopCheckEntity.setUserId(userid);
            shopCheckEntity.setLastTime(nowdate);
//          增加10积分
            ShopUserEntity shopUserEntity1 = shopUserDao.selectById(userid);
            shopUserEntity1.setUserIntegral(10);
            shopUserDao.updateById(shopUserEntity1);
            shopCheckDao.insert(shopCheckEntity);
            return R.ok("签到成功，用户第一次签到，赠送10积分");
        }
        Integer Signday = byId.getLastTime();//最后签到日期

        int i = Integer.parseInt(datestring);//日期转换三位整数


        if (byId==null || Signday-nowdate>1){
            //用户第一次签到
            ShopCheckEntity shopCheckEntity = new ShopCheckEntity();
            shopCheckEntity.setContinuitySign(1);
            shopCheckEntity.setUserId(userid);
            shopCheckEntity.setLastTime(nowdate);
//          增加10积分
            return firsSign(shopCheckEntity,userid);
        }


        Integer continuitySign = byId.getContinuitySign();//获取连续签到天数
        Integer jifen=10+(continuitySign-1)*15;//连续签到计算公式

        //判断是否重复签到
        if (byId.getLastTime()==nowdate){
            return R.error("抱歉，您今天已经签到过了");
        }

        //判断两次签到是否不符
        if (Signday>nowdate){
            return R.ok("您签到信息 错误，请联系管理员");
        }

        //判断跨年签到是否断签
        if (datestring.equals("0101") && !Signday.equals("1231") && byId!=null){
//          增加10积分
            ShopUserEntity shopUserEntity1 = shopUserDao.selectById(userid);
            shopUserEntity1.setUserIntegral(10);
            shopUserDao.updateById(shopUserEntity1);
            return R.ok("签到成功，用户连续一天签到，赠送10积分");

        }
        //判断跨年签到是否连续签到
        if (datestring.equals("0101") && Signday.equals("1231")  && byId!=null){
            ShopUserEntity shopUserEntity1 = shopUserDao.selectById(userid);
            shopUserEntity.setUserIntegral(shopUserEntity1.getUserIntegral()+jifen);
            shopUserDao.updateById(shopUserEntity);
            return R.ok("签到成功，您当前签到"+continuitySign+"天，赠送"+jifen+"积分");
        }


        //相减查看是否连续签到
        if (nowdate-Signday!=1){
            byId.setContinuitySign(1);//非连续签到，连续签到日期设置为1天
            byId.setLastTime(nowdate);
        }

        //增加连续签到天数
        if (i-Signday==1){
            byId.setContinuitySign(byId.getContinuitySign()+1);
            byId.setLastTime(nowdate);
        }


        //更新签到表
        this.updateById(byId);

        //增加规定积分
        ShopUserEntity shopUserEntity1 = shopUserDao.selectById(shopUserEntity.getUserId());
        shopUserEntity.setUserIntegral(shopUserEntity1.getUserIntegral()+jifen);
        shopUserDao.updateById(shopUserEntity);
        return R.ok("签到成功，您当前签到"+continuitySign+"天，赠送"+jifen+"积分");
    }

    public R firsSign( ShopCheckEntity shopCheckEntity,Integer userid ){
//          增加10积分
        ShopUserEntity shopUserEntity1 = shopUserDao.selectById(userid);
        shopUserEntity1.setUserIntegral(10);
        shopUserDao.updateById(shopUserEntity1);
        this.save(shopCheckEntity);
        return R.ok("签到成功，用户连续一天签到，赠送10积分");
    }

}